package com.googlecode.garbagecan.simonstudy.myspring;

import java.io.Serializable;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.javasimon.SimonManager;
import org.javasimon.Split;
import org.springframework.aop.support.AopUtils;

public final class MonitoringInterceptor implements MethodInterceptor, Serializable {

	private static final long serialVersionUID = 1L;

	public Object invoke(MethodInvocation invocation) throws Throwable {
		String className = AopUtils.getTargetClass(invocation.getThis()).getName();
		String methodName = invocation.getMethod().getName();
		String name = className + "." + methodName;
		
		Split split = SimonManager.getStopwatch(name).start();
		try {
			return invocation.proceed();
		} finally {
			split.stop();
		}
	}
}